package com.sky.service.impl;

import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.mapper.OrderDetailMapper;
import com.sky.mapper.OrdersMapper;
import com.sky.service.DataStatisticsService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class DataStatisticsServiceImpl implements DataStatisticsService {


    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private OrdersMapper ordersMapper;


    /**
     * 查询销量排名top10
     *
     * @return
     */
    @Override
    public SalesTop10ReportVO getTop10(LocalDate begin, LocalDate end) {
        //菜名
        List<Orders> orders = ordersMapper.getOrdersByTime(begin, end);

        List<Long> orderIds = orders.stream().map(Orders::getId).collect(Collectors.toList());
        StringBuilder dishName = new StringBuilder();
        StringBuilder dishNumber = new StringBuilder();
        List<OrderDetail> orderDetailList = new ArrayList<>();

        Comparator<Integer> keyComparator = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2.compareTo(o1); // 降序排序
            }
        };
        TreeMap<String, Integer> nameAndNumber = new TreeMap<>();

        for (Long orderId : orderIds) {

            List<OrderDetail> orderDetails = orderDetailMapper.getorderDetailById(orderId);
            orderDetailList.addAll(orderDetails);
        }
        log.info("orderDetailList.size{}",orderDetailList.size());

        for (OrderDetail orderDetail : orderDetailList) {

            if (nameAndNumber.containsKey(orderDetail.getName())) {

                Integer value = nameAndNumber.get(orderDetail.getName()) + orderDetail.getNumber();

                nameAndNumber.put(orderDetail.getName(), value);
            } else {
                nameAndNumber.put(orderDetail.getName(), orderDetail.getNumber());
            }

        }

        nameAndNumber.forEach(
                (String dish, Integer count) -> {
                    dishName.append(dish).append(",");
                    dishNumber.append(count).append(",");
                }
        );

        return new SalesTop10ReportVO(dishName.toString(),dishNumber.toString());
    }

    /**
     * 用户统计
     *
     * @return
     */
    @Override
    public UserReportVO userStatistics(LocalDate begin, LocalDate end) {

        List<Orders> orders = ordersMapper.getOrdersByTime(begin, end);

        StringBuilder date = new StringBuilder();
        StringBuilder user = new StringBuilder();
        LinkedHashMap<LocalDate,Integer> dateAndUser = new LinkedHashMap<>();
        Map<Long,Integer> userCount = new HashMap<>();
        for (Orders order : orders) {

            if (userCount.containsKey(order.getUserId())) {

                userCount.put(order.getUserId(),userCount.get(order.getUserId())+1);

            } else {
                userCount.put(order.getUserId(),0);
            }
        }

        for (Orders order : orders) {

            LocalDate newDate = order.getOrderTime().toLocalDate();

            if (dateAndUser.containsKey(newDate)) {
                Long userId = null;
                Integer value=0;
                if(Objects.equals(userId,order.getUserId())){
                    value++;
                }

                dateAndUser.put(newDate, value);
            } else {
                dateAndUser.put(newDate, 0);
            }
        }


        return null;
    }

    /**
     * 营业额统计
     *
     * @return
     */
    @Override
    public TurnoverReportVO turnoverStatistics(LocalDate begin, LocalDate end) {
        List<Orders> orders = ordersMapper.getOrdersByTime(begin, end);
        return null;
    }

    /**
     * 订单统计
     *
     * @return
     */
    @Override
    public OrderReportVO ordersStatistics(LocalDate begin, LocalDate end) {
        return null;
    }
}
